{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 安装\n",
    "\n",
    ":label:`chap_installation`\n",
    "\n",
    "为了深入学习 `DJL`，本书嵌入了许多可运行的 Java 示范用例。在学习过程中，我们需要搭建一个 `DJL` 的运行及学习环境。这个学习运行环境包括 Python, Jupyter Notebooks 以及相关的函数库及软件包。\n",
    "\n",
    "\n",
    "## 安装 JDK 11 (不是 JRE)\n",
    "\n",
    "本书中提供的所有程序示例，只能在 JDK 11 (或以上) 版本运行。\n",
    "\n",
    "Java 运行路径是一个非常重要的环境配置。在 `DJL` 运行环境安装的过程中，Java 运行路径是否配置正确。检查的方法是运行下面的命令行。如果运行结果如下面显示的，并且版本号为11或以上，那你的环境就配置正确了。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```bash\n",
    "java --list-modules | grep \"jdk.jshell\"\n",
    "\n",
    "> jdk.jshell@12.0.1\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在 Python 环境下安装 Jupyter Notebook\n",
    "\n",
    "在 Python3 的环境下，用下面的命令行安装 Jupyter Notebook:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```bash\n",
    "pip3 install jupyter\n",
    "\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装 IJava 内核\n",
    "为了运行本书中嵌入的 Java 示范用例，我们需要在 Jupyter Notebook 运行环境下添加安装 IJava 内核。安装步骤如下：\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```bash\n",
    "git clone https://github.com/frankfliu/IJava.git\n",
    "cd IJava/\n",
    "./gradlew installKernel\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下载 D2L-Java 记事本文件\n",
    "\n",
    "本书中的 Jupyter 记事本文件 可以从我们的 [Github](https://github.com/deepjavalibrary/d2l-java) 代码仓库里克隆拷贝。下面是克隆拷贝的命令行。拷贝到本地后，你就可以用 Jupyter Notebook 对教程中的各个章节进行浏览了。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```bash\n",
    "git clone https://github.com/deepjavalibrary/d2l-java\n",
    "cd d2l-java\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运行 Jupyter 记事本文件\n",
    "如果前面的步骤都已经顺利完成，接下来我们就可以用下面的命令行运行 Jupyter Notebook 了:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```bash\n",
    "jupyter notebook\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Jupyter Notebook 启动后，在浏览器输入 http://localhost:8888 本教程就在浏览器中打开了。学员在学习过程中，可以运行教程中嵌入的 Java 示范用例，对所学的内容加强理解。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 添加 DJL 的依赖项\n",
    "\n",
    "下面是添加 `DJL` 的 maven 依赖项的必要设置：\n",
    "\n",
    "```java\n",
    "%maven ai.djl:api:0.11.0\n",
    "%maven org.slf4j:slf4j-simple:1.7.26\n",
    "        \n",
    "%maven ai.djl.mxnet:mxnet-engine:0.11.0\n",
    "%maven ai.djl.mxnet:mxnet-native-auto:1.8.0\n",
    "```\n",
    "\n",
    "为了避免重复，我们把 maven 下载和常用类库引入的代码存到 ../utils/djl-import.ipynb 文件中，这里我们只要使用 %load 宏调用即可。\n",
    "\n",
    "```\n",
    "%load ../utils/djl-imports.ipynb\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "现在，你可以导入并运行 DJL 的各个模块了。\n",
    "\n",
    "\n",
    "## GPU 支持\n",
    "\n",
    "\n",
    ":label:`subsec_gpu`\n",
    "\n",
    "\n",
    "我们推荐使用 \"ai.djl.mxnet:mxnet-native-auto\" 依赖。当运行环境中使用了 \"ai.djl.mxnet:mxnet-native-auto\" 依赖，DJL 会自动检测你的操作系统是否有 GPU，如果发现系统有 GPU，DJL 会自动下载与 GPU 相匹配的 MXNet GPU 本地库。这个自动功能，省却学员手工配置 MXNet 引擎的麻烦。对于高阶学员，希望手工配置 DJL 的 MXNet 引擎运行环境，可以参考我们的 [文档](https://github.com/deepjavalibrary/djl/tree/master/mxnet/mxnet-engine)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "## 练习\n",
    "\n",
    "1. 下载 `DJL` 教程，并安装教程的运行环境。\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
